package cn.jwcat.controller;

import cn.jwcat.dealEntity.ClaimDto;
import cn.jwcat.dealEntity.DealClaim;
import cn.jwcat.entity.Claim;
import cn.jwcat.entity.Staff;
import cn.jwcat.service.IClaimService;
import cn.jwcat.utils.JwtTokenUtil;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 物资申领操作
 */
@Transactional
@RestController
@RequestMapping("/claim")
public class ClaimController {
	@Resource
	IClaimService claimService;

	@Resource
	JwtTokenUtil jwtTokenUtil;

	@GetMapping("/{page}/{size}")
	@ApiOperation("获取物资申领的全部列表")
	public Result getAll(@PathVariable("page") int page, @PathVariable("size") int size) {
		PageHelper.startPage(page, size);
		List<ClaimDto> claimDtos = claimService.getAll();
		PageInfo<ClaimDto> info = new PageInfo<>(claimDtos);
		return Result.success(info);
	}

	@PostMapping
	@ApiOperation("提交物资申请")
	public Result postClaimDto(@RequestBody ClaimDto claimDto, HttpServletRequest request) {
		Staff staff = jwtTokenUtil.getStaffFromRequest(request);
		return claimService.postClaimDto(claimDto, staff);
	}

	@PutMapping
	@ApiOperation("修改物资申领状态")
	public Result putDealClaim(@RequestBody DealClaim dealClaim) {
		Boolean bool = claimService.putDealClaim(dealClaim);
		if (bool) return Result.success();
		return Result.error();
	}

	@PostMapping("/query/{page}/{size}")
	@ApiOperation("模糊查询物资申领的列表")
	public Result queryByStatus(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody DealClaim dealClaim) {
		PageHelper.startPage(page, size);
		List<ClaimDto> dtos = claimService.queryByStatus(dealClaim);
		PageInfo<ClaimDto> info = new PageInfo<>(dtos);
		return Result.success(info);
	}

	@GetMapping
	@ApiOperation("获取志愿者申领的物资")
	public Result getClaim(HttpServletRequest request) {
		Staff staff = jwtTokenUtil.getStaffFromRequest(request);
		List<ClaimDto> claimDtos = claimService.getClaim(staff.getStaffId());
		return Result.success(claimDtos);
	}

	@PostMapping("/phone")
	@ApiOperation("用户提交物资申请")
	public Result postClaimDto(@RequestBody Claim claim) {
		boolean save = claimService.save(claim);
		if (save) return Result.success("申请成功");
		return Result.error().message("物资申请失败");
	}

	@GetMapping("/phone")
	@ApiOperation("获取用户申领的物资")
	public Result getUserClaim(HttpServletRequest request) {
		Integer id = jwtTokenUtil.getIDFromRequest(request);
		List<ClaimDto> claimDtos = claimService.getPhone(id);
		return Result.success(claimDtos);
	}

	@DeleteMapping("/{id}")
	@ApiOperation("根据id删除申请")
	public Result delete(@PathVariable String id) {
		LambdaQueryWrapper<Claim> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Claim::getClaimId, id);
		boolean remove = claimService.remove(wrapper);
		if (remove) return Result.success("删除成功");
		return Result.error().message("删除失败");
	}
}
